<script setup lang="ts">
import {getIdentityListAPI, addIdentityAPI, deleteIdentityAPI} from "@/api/user.ts";
import {onMounted, reactive, ref} from "vue";
import {formattedDate} from "@/utils/formatDate.ts";
import {Delete, Edit, InfoFilled} from "@element-plus/icons-vue";
import {ElMessage, ElNotification} from "element-plus";
import Pager from "@/components/Pager.vue";
import {identityItem} from "@/type/user";

const identityList = ref<identityItem[]>()
const dialogVisible = ref(false)
const addIdentityData = reactive({
  name: ''
})

const pagerParams = reactive({
  limit: 8,
  page: 1
})

let total = ref()

// 获取身份列表
const getIdentityList = async () => {
  const res = await getIdentityListAPI(pagerParams)
  identityList.value = res.data.identitys
  total.value = res.data.total
}

// 添加身份
const addIdentity = async () => {
  const res = await addIdentityAPI(addIdentityData)
  if (res.code === 0) {
    dialogVisible.value = false
    ElMessage({
      type: 'success',
      message: '添加成功'
    })
    await getIdentityList()
  }
}

// 编辑身份
const EditIdentity = () => {
  ElNotification({
    title: 'Warning',
    message: '为减缓服务器压力，暂关闭编辑接口及数据回显',
    type: 'warning',
  })
}

// 删除身份
const deleteIdentity = async (data) => {
  await deleteIdentityAPI(data._id)
  ElMessage({
    type: 'success',
    message: '删除成功'
  })
  await getIdentityList()
}

const handleCurrentPageChange = async (newPage) => {
  pagerParams.page = newPage
  await getIdentityList()
}

onMounted(() => {
  getIdentityList()
})
</script>

<template>
  <el-card class="box-card">
    <div class="card-identity-title">
      <el-button type="primary" @click="dialogVisible = true">添加身份</el-button>
    </div>
    <div class="card-identity-table">
      <el-table
          border
          ref="multipleTableRef"
          :data="identityList"
          style="width: 100%"
      >
        <el-table-column label="#" width="100" align="center" type="index"/>
        <el-table-column prop="_id" align="center" label="id"/>
        <el-table-column prop="name" align="center" label="名称"/>
        <el-table-column prop="createdAt" align="center" label="创建时间">
          <template #default="{row}">
            {{ formattedDate(row.createdAt) }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="控制" width="260">
          <template #default="{row}">
            <div class="operator-btn">
              <el-button type="primary" size="default" @click="EditIdentity" :icon="Edit as any" circle/>
              <el-popconfirm
                  width="200"
                  @confirm="deleteIdentity(row)"
                  confirm-button-text="确定"
                  cancel-button-text="取消"
                  :icon="InfoFilled as any"
                  icon-color="#f36b6c"
                  title="您确定删除此身份吗?"
              >
                <template #reference>
                  <el-button type="danger" size="default" :icon="Delete as any" circle/>
                </template>
              </el-popconfirm>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog v-model="dialogVisible" title="添加身份">
      <div class="row align_center">
        <span style="width: 80px">请输入名称</span>
        <el-input style="margin-left: 20px" v-model="addIdentityData.name"/>
      </div>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="addIdentity">
          确定
        </el-button>
      </span>
      </template>
    </el-dialog>
    <Pager :size="pagerParams.limit"
           :total="total"
           :currentPage="pagerParams.page"
           @currentPageChange="handleCurrentPageChange"
    />
  </el-card>
</template>

<style scoped lang="scss">
.box-card {
  padding: 0 50px;

  .card-identity-title {
    display: flex;
    justify-content: flex-end;
  }

  .card-identity-table {
    margin-top: 20px;
  }
}
</style>